using System;
using System.Collections;
using System.Web;

using Deepcode.Flux.Core.Services.XML;
using Deepcode.Flux.Core.Systems.Workflow.Bases;
using Deepcode.Flux.Core.Systems.Workflow.Interfaces;

namespace Deepcode.Flux.Core.Systems.Workflow.BaseHandlers
{
	/// <summary>
	/// Exposes the configuration form for the GotoProcess standard workflow action
	/// </summary>
	public class GotoProcessConfig: IWorkflowHandlerMethodConfig
	{
		/// <summary>
		/// Will hold the controls for the options
		/// </summary>
		ArrayList controls = new ArrayList();
		// Control declarations
		Deepcode.Flux.Core.UI.Controls.ShadePadBox spb;
		System.Web.UI.WebControls.Table ContainerTable;
		System.Web.UI.WebControls.TableRow ContainerRow;
		System.Web.UI.WebControls.TableCell CaptionCell;
		System.Web.UI.WebControls.TableCell DataCell;
		System.Web.UI.WebControls.DropDownList OtherProcesses;
		/// <summary>
		/// Build the configuration form
		/// </summary>
		/// <param name="HandlerID"></param>
		/// <param name="DefinitionID"></param>
		/// <param name="ProcessID"></param>
		/// <returns></returns>
		public System.Collections.ArrayList AddFormControls(System.Web.UI.Page CallingPage, string HandlerID, int DefinitionID, int ProcessID)
		{
			controls.Clear();
			// Setup the main container
			spb = new Deepcode.Flux.Core.UI.Controls.ShadePadBox();
			// Setup the table, row and two cells
			ContainerTable = new System.Web.UI.WebControls.Table();
			ContainerRow = new System.Web.UI.WebControls.TableRow();
			CaptionCell = new System.Web.UI.WebControls.TableCell();
			DataCell = new System.Web.UI.WebControls.TableCell();
			// Generate links between them
			ContainerTable.Controls.Add( ContainerRow );
			ContainerRow.Controls.Add( CaptionCell );
			ContainerRow.Controls.Add( DataCell );
			// Setup the caption cell
			CaptionCell.Text = "Select process to open:";
			CaptionCell.CssClass = "FormCaption";
			// Create the drop down list
			OtherProcesses = new System.Web.UI.WebControls.DropDownList();
			// Load all the processes within the definition
			Processes processes = new Processes();
			Process [] ps = processes.GetProcessByDefinitionID( DefinitionID );
			foreach( Process p in ps )
				OtherProcesses.Items.Add( new System.Web.UI.WebControls.ListItem( p.ProcessName, p.PK_ID.ToString() ));
			// Add the DDL to the cell
			DataCell.Controls.Add( OtherProcesses );
			// Add the Table to the control array
			spb.Controls.Add( ContainerTable );
			controls.Add( spb );
			return controls;
		}
		/// <summary>
		/// Serialise the configuration form to XML
		/// </summary>
		/// <returns></returns>
		public string Serialise()
		{
			PropertyBag pb = new PropertyBag();
			pb.Add("GotoProcessID", OtherProcesses.SelectedValue);
			return pb.Serialise();
		}
		/// <summary>
		/// Deserialise the XML to the configuration form
		/// </summary>
		/// <param name="XmlDatapacket"></param>
		public void DeSerialise(string XmlDatapacket)
		{
			PropertyBag pb = new PropertyBag( XmlDatapacket );
			try{ OtherProcesses.SelectedValue = pb.Get("GotoProcessID", ""); }
			catch {}
		}
	}
}
